<template>
  <el-dialog
    :title="!dataForm.houseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="楼盘id" prop="houseNameId">
      <el-input v-model="dataForm.houseNameId" placeholder="楼盘id"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="houseAddress">
      <el-input v-model="dataForm.houseAddress" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="租金" prop="housePrice">
      <el-input v-model="dataForm.housePrice" placeholder="租金"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="houseSort">
      <el-input v-model="dataForm.houseSort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="houseDate">
      <el-input v-model="dataForm.houseDate" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="房屋状态（0为已出租，1为未出租）" prop="houseStatus">
      <el-input v-model="dataForm.houseStatus" placeholder="房屋状态（0为已出租，1为未出租）"></el-input>
    </el-form-item>
    <el-form-item label="经纪人id" prop="houseAgent">
      <el-input v-model="dataForm.houseAgent" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="房屋描述" prop="houseDescription">
      <el-input v-model="dataForm.houseDescription" placeholder="房屋描述"></el-input>
    </el-form-item>
    <el-form-item label="房屋平米" prop="houseSquaremetre">
      <el-input v-model="dataForm.houseSquaremetre" placeholder="房屋平米"></el-input>
    </el-form-item>
    <el-form-item label="房屋锁" prop="houseLocking">
      <el-input v-model="dataForm.houseLocking" placeholder="房屋锁"></el-input>
    </el-form-item>
    <el-form-item label="标签" prop="houseLabel">
      <el-input v-model="dataForm.houseLabel" placeholder="标签"></el-input>
    </el-form-item>
    <el-form-item label="所在楼层" prop="houseFloor">
      <el-input v-model="dataForm.houseFloor" placeholder="所在楼层"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="houseTitle">
      <el-input v-model="dataForm.houseTitle" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="出租描述" prop="userHouseDescription">
      <el-input v-model="dataForm.userHouseDescription" placeholder="出租描述"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除（1.删除，0.展示）" prop="houseDelete">
      <el-input v-model="dataForm.houseDelete" placeholder="逻辑删除（1.删除，0.展示）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          houseId: 0,
          houseNameId: '',
          houseAddress: '',
          housePrice: '',
          houseSort: '',
          houseDate: '',
          houseStatus: '',
          houseAgent: '',
          houseDescription: '',
          houseSquaremetre: '',
          houseLocking: '',
          houseLabel: '',
          houseFloor: '',
          houseTitle: '',
          userHouseDescription: '',
          userId: '',
          houseDelete: ''
        },
        dataRule: {
          houseNameId: [
            { required: true, message: '楼盘id不能为空', trigger: 'blur' }
          ],
          houseAddress: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          housePrice: [
            { required: true, message: '租金不能为空', trigger: 'blur' }
          ],
          houseSort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          houseDate: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          houseStatus: [
            { required: true, message: '房屋状态（0为已出租，1为未出租）不能为空', trigger: 'blur' }
          ],
          houseAgent: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          houseDescription: [
            { required: true, message: '房屋描述不能为空', trigger: 'blur' }
          ],
          houseSquaremetre: [
            { required: true, message: '房屋平米不能为空', trigger: 'blur' }
          ],
          houseLocking: [
            { required: true, message: '房屋锁不能为空', trigger: 'blur' }
          ],
          houseLabel: [
            { required: true, message: '标签不能为空', trigger: 'blur' }
          ],
          houseFloor: [
            { required: true, message: '所在楼层不能为空', trigger: 'blur' }
          ],
          houseTitle: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          userHouseDescription: [
            { required: true, message: '出租描述不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          houseDelete: [
            { required: true, message: '逻辑删除（1.删除，0.展示）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.houseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.houseId) {
            this.$http({
              url: this.$http.adornUrl(`/house/leasehome/info/${this.dataForm.houseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.houseNameId = data.leaseHome.houseNameId
                this.dataForm.houseAddress = data.leaseHome.houseAddress
                this.dataForm.housePrice = data.leaseHome.housePrice
                this.dataForm.houseSort = data.leaseHome.houseSort
                this.dataForm.houseDate = data.leaseHome.houseDate
                this.dataForm.houseStatus = data.leaseHome.houseStatus
                this.dataForm.houseAgent = data.leaseHome.houseAgent
                this.dataForm.houseDescription = data.leaseHome.houseDescription
                this.dataForm.houseSquaremetre = data.leaseHome.houseSquaremetre
                this.dataForm.houseLocking = data.leaseHome.houseLocking
                this.dataForm.houseLabel = data.leaseHome.houseLabel
                this.dataForm.houseFloor = data.leaseHome.houseFloor
                this.dataForm.houseTitle = data.leaseHome.houseTitle
                this.dataForm.userHouseDescription = data.leaseHome.userHouseDescription
                this.dataForm.userId = data.leaseHome.userId
                this.dataForm.houseDelete = data.leaseHome.houseDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/leasehome/${!this.dataForm.houseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'houseId': this.dataForm.houseId || undefined,
                'houseNameId': this.dataForm.houseNameId,
                'houseAddress': this.dataForm.houseAddress,
                'housePrice': this.dataForm.housePrice,
                'houseSort': this.dataForm.houseSort,
                'houseDate': this.dataForm.houseDate,
                'houseStatus': this.dataForm.houseStatus,
                'houseAgent': this.dataForm.houseAgent,
                'houseDescription': this.dataForm.houseDescription,
                'houseSquaremetre': this.dataForm.houseSquaremetre,
                'houseLocking': this.dataForm.houseLocking,
                'houseLabel': this.dataForm.houseLabel,
                'houseFloor': this.dataForm.houseFloor,
                'houseTitle': this.dataForm.houseTitle,
                'userHouseDescription': this.dataForm.userHouseDescription,
                'userId': this.dataForm.userId,
                'houseDelete': this.dataForm.houseDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
